//
//  RatingBar.m
//  easymarketing
//
//  Created by chesentao
//  Copyright (c) 2015年 cubead. All rights reserved.
//
#import "Toast.h"
#import "InsetsLabel.h"

@interface Toast()
@property(nonatomic,strong)InsetsLabel * titleLabel;
@end

@implementation Toast

+(Toast *)makeText:(NSString *)text
{
    CGSize screenize=[UIScreen mainScreen].bounds.size;
    static Toast * toast=nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        toast=[[self alloc]init];
        toast.titleLabel=[[InsetsLabel alloc] initWithInsets:UIEdgeInsetsMake(7, 10, 7, 10)];
        toast.titleLabel.textColor=[UIColor whiteColor];
        toast.titleLabel.textAlignment=NSTextAlignmentCenter;
        toast.titleLabel.layer.masksToBounds=YES;
        toast.titleLabel.numberOfLines=0;
        toast.titleLabel.font=[UIFont systemFontOfSize:14];
        toast.titleLabel.layer.cornerRadius=5;
        toast.titleLabel.alpha=1.0f;
        toast.titleLabel.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:0.75f];
    });
    //    if(text.length>15)
    //    {
    //        text=[NSString stringWithFormat:@"%@...",[text substringToIndex:12]];
    //    }
    CGSize size=[text boundingRectWithSize:CGSizeMake(screenize.width-60, 100) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
    size=CGSizeMake(size.width+20, size.height+14);
    toast.titleLabel.frame=CGRectMake((screenize.width-size.width)/2, screenize.height/5.0f, size.width, size.height);
    toast.titleLabel.text=text;
    return toast;
}
-(void)show
{
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    [window addSubview:self.titleLabel];
    [window bringSubviewToFront:self.titleLabel];
    self.titleLabel.alpha=0;
    [UIView animateWithDuration:0.2f delay:0 options:0 animations:^{
        self.titleLabel.alpha=1;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.3f delay:1.2f options:0 animations:^{
            self.titleLabel.alpha=0;
        } completion:^(BOOL finished) {
            self.titleLabel.alpha=1;
            [self.titleLabel removeFromSuperview];
        }];
    }];
}
@end
